package com.xyrl.project.common.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyrl.project.common.common.enu.AttImageUseType;
import com.xyrl.project.common.common.service.AttachmentService;
import com.xyrl.project.common.store.dao.StoreLicenseMapper;
import com.xyrl.project.common.store.entity.StoreLicense;
import com.xyrl.project.common.store.enu.StoreAuthStatus;
import com.xyrl.project.common.store.enu.StoreAuthType;
import com.xyrl.project.common.store.service.StoreAuthService;
import com.xyrl.project.common.store.service.StoreLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


/**
* @className StoreLicenseServiceImpl.java
* @company 重庆星纭融链科技公司
* @author 徐恩源
* @version 1.0
* @date 2019-12-30 10:32:48
* @Description 营业执照serviceImpl
*/
@Service
public class StoreLicenseServiceImpl extends ServiceImpl<StoreLicenseMapper, StoreLicense> implements StoreLicenseService {
    @Autowired
    private StoreAuthService storeAuthService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public boolean saveStoreLicense(StoreLicense storeLicense) {
        int count = super.count(new LambdaQueryWrapper<StoreLicense>().eq(StoreLicense::getId, storeLicense.getId()));
        Assert.isTrue(count == 0, "营业执照已认证,请勿重复认证");
        //保存营业执照
        super.saveOrUpdate(storeLicense);
        // 保存认证信息
        storeAuthService.updateStoreAuth(storeLicense.getId(), storeLicense.getCreateUserId(),
                StoreAuthType.licenseAuth, StoreAuthStatus.YES);

        attachmentService.handleAttImageUsePath(storeLicense.getId(), AttImageUseType.licenseAuth, storeLicense.getPath(), null);
        return true;
    }
}
